import {login, logout, getInfo} from "../../api/user";
import {getToken, setToken, removeToken} from '../../utils/auth'
import {Notification} from 'element-ui'
import store from "../index";

const user = {
    state: {
        token: getToken(),
        name: '',
        avatar: '',
        roles: [],
        resource: []
    },

    mutations: {
        SET_TOKEN: (state, token) => {
            state.token = token
        },
        SET_NAME: (state, name) => {
            state.name = name
        },
        SET_AVATAR: (state, avatar) => {
            state.avatar = avatar
        },
        SET_ROLES: (state, roles) => {
            state.roles = roles
        },
        SET_RESOURCE: (state, resource) => {
            state.resource = resource
        }
    },

    actions: {
        //登录
        Login({commit}, userInfo) {
            return new Promise((resolve, reject) => {
                login(userInfo).then(res => {
                    const token = res;
                    setToken(token)
                    commit('SET_TOKEN', token)
                    resolve()
                }).catch(err => {
                    reject(err);
                })
            })
        },

        //获取用户信息
        GetInfo({commit, state}) {
            return new Promise((resolve, reject) => {
                getInfo().then(res => {
                    const data = res
                    if (data.roles && data.roles.length > 0) {
                        commit("SET_ROLES", data.roles);
                    }
                    if (data.menuList && data.menuList.length > 0) {
                        commit("SET_RESOURCE", data.menuList);
                    } else {
                        reject('getInfo: roles must be a non-null array !')
                    }
                    commit("SET_NAME", data.username)
                    if (data.icon !== null) {
                        commit("SET_AVATAR", data.icon)
                    }
                    resolve(res)
                }).catch(err => {
                    reject(err)
                })
            })
        },

        // 退出登录
        Logout({commit, state}) {
            return new Promise((resolve, reject) => {
                logout(state.token).then((res) => {
                    commit('SET_TOKEN', '')
                    commit('SET_ROLES', [])
                    commit('SET_RESOURCE', [])
                    removeToken()
                    resolve()
                }).catch(err => {
                    reject(err);
                }).finally(() => {
                    Notification({
                        message: '登出成功',
                        type: 'success',
                        duration: 3 * 1000
                    })
                })
            })
        },

        // 前端 登出
        FedLogOut({commit}) {
            return new Promise(resolve => {
                commit('SET_TOKEN', '')
                removeToken()
                resolve()
            })
        }
    }
}

export default user;